package com.example.newnoke.ui;

import android.databinding.DataBindingUtil;
import android.os.CountDownTimer;
import android.view.View;

import com.example.newnoke.R;
import com.example.newnoke.data.BaseDatas;
import com.example.newnoke.data.LoginData;
import com.example.newnoke.databinding.ActivityZhaohuiBinding;
import com.example.newnoke.mvp.presenter.LoginPresenter;
import com.example.newnoke.mvp.view.ILoginV;
import com.example.newnoke.utils.API;
import com.geya.jbase.baseactivity.BaseDetailsActivity;
import com.geya.jbase.constant.BaseData;
import com.geya.jbase.constant.RequestType;
import com.geya.jbase.uiview.ToastUtil;

import java.util.HashMap;

public class ZHMMActivity extends BaseDetailsActivity<LoginPresenter> implements ILoginV {

    private ActivityZhaohuiBinding mBinding;


    private CountDownTimer timer;

    @Override
    public LoginPresenter newPresenter() {
        return  new LoginPresenter(this);
    }

    @Override
    public void init() {
        timer = new CountDownTimer(60*1000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mBinding.btnYzm.setText(millisUntilFinished/1000+"秒后重发");
            }

            @Override
            public void onFinish() {
                mBinding.btnYzm.setText("获取验证码");
                mBinding.btnYzm.setClickable(true);
            }
        };
        initOnClick();

    }

    @Override
    public void setContentView() {
        mImmersionBar.statusBarColor(R.color.wu);
        mImmersionBar.init();
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_zhaohui);
    }


    @Override
    public void login(LoginData.DataBean.PopInfoBean data) {
        ToastUtil.showShort("密码找回成功！");
        finish();
    }

    @Override
    public void showMsg(String msg) {
        ToastUtil.showShort(msg);
    }

    public void initOnClick(){
        mBinding.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBinding.btnYzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mBinding.tvShouij.getText().toString().isEmpty()){
                    ToastUtil.showShort("请输入手机号");
                    return;
                }
                timer.start();
                mBinding.btnYzm.setClickable(false);
                HashMap<String, String> map = new HashMap<>();
                map.put("mobile",mBinding.tvShouij.getText().toString());//13593236985
                mPresenter.accessServers(RequestType.OKGO_POST, API.SMSCODE, BaseData.class, map);
            }
        });
        mBinding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mBinding.tvYam.getText().toString().isEmpty()){
                    ToastUtil.showShort("请输入验证码");
                    return;
                }  if (mBinding.tvShouij.getText().toString().isEmpty()){
                    ToastUtil.showShort("请输入账号");
                    return;
                }  if (mBinding.tvMima.getText().toString().isEmpty()){
                    ToastUtil.showShort("请输入密码");
                    return;
                }
                HashMap<String, String> map = new HashMap<>();
                map.put("mobile",mBinding.tvShouij.getText().toString());
                map.put("code",mBinding.tvYam.getText().toString());
                map.put("newpwd",mBinding.tvMima.getText().toString());
                mPresenter.accessServers(RequestType.OKGO_POST, API.FINDPWD, BaseDatas.class, map);
            }
        });
    }
}
